package com.lanou.sfu.songforu.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.adapter.MenuAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.base.Constants;
import com.lanou.sfu.songforu.view.MyEvent;
import com.lanou.sfu.songforu.view.ReplaceFragment;

import de.greenrobot.event.EventBus;

/**
 * Created by quyan on 15/10/24.
 */
public class MainPagerFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener, View.OnClickListener {
    //定义viewPager对象
    private ViewPager vp_content;
    private RadioGroup rg_main_tab;
    private RadioButton rb_mine, rb_recommend, rb_singers,
            rb_rank;
    private MenuAdapter menuAdapter;
    private ImageView search;
    private ImageView menuBtn;
    private boolean is_closed = false;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.title_include, null);
        menuBtn = (ImageView) view.findViewById(R.id.menuBtn);
        initView(view);
        return view;
    }
    public void onEvent(MyEvent.More more){
        if (more.getMessage()==1){
        vp_content.setCurrentItem(2);}
        else if (more.getMessage()==2){
            vp_content.setCurrentItem(2);
        }
    }

    private void initView(View view) {
        //初始化viewPager
        vp_content = (ViewPager) view.findViewById(R.id.vp_content);
        //初始化RadiosGroup
        rg_main_tab = (RadioGroup) view.findViewById(R.id.rg_main_tab);
        rb_mine = (RadioButton) view.findViewById(R.id.rb_mine);
        rb_recommend = (RadioButton) view.findViewById(R.id.rb_recommend);
        rb_singers = (RadioButton) view.findViewById(R.id.rb_singers);
        rb_rank = (RadioButton) view.findViewById(R.id.rb_rank);
        search = (ImageView) view.findViewById(R.id.secrch);
        menuAdapter = new MenuAdapter(getChildFragmentManager());
        vp_content.setAdapter(menuAdapter);
        vp_content.setCurrentItem(0);

        //设置监听
        rg_main_tab.setOnCheckedChangeListener(this);
        vp_content.setOnPageChangeListener(this);
        menuBtn = (ImageView) view.findViewById(R.id.menuBtn);
        search = (ImageView) view.findViewById(R.id.secrch);
        menuBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        search = (ImageView) view.findViewById(R.id.secrch);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchFragment fragment = new SearchFragment();
                new ReplaceFragment(fragment, getFragmentManager(),R.id.fl_main,true,null);

            }
        });
    }








    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_mine:
                vp_content.setCurrentItem(Constants.PAGE_MINE);
                break;
            case R.id.rb_recommend:
                vp_content.setCurrentItem(Constants.PAGE_RECOMMEND);
                break;
            case R.id.rb_singers:
                vp_content.setCurrentItem(Constants.PAGE_SINGERS);
                break;
            case R.id.rb_rank:
                vp_content.setCurrentItem(Constants.PAGE_RANK);
                break;

        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (myPageChangeListener != null)
            myPageChangeListener.onPageSelected(position);

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                rb_mine.setChecked(true);
                break;
            case 1:
                rb_recommend.setChecked(true);
                break;
            case 2:
                rb_singers.setChecked(true);
                break;
            case 3:
                rb_rank.setChecked(true);
                break;

        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // state的状态有三个，0表示什么都没做，1正在滑动，2滑动完毕
        /*if (state == 2) {
            switch (vp_content.getCurrentItem()) {
                default:
                    break;
            }
        }*/
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        menuBtn.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.menuBtn:
                ((MainActivity) getActivity()).showLeft();
                break;
        }
    }

    public boolean isFirst() {
        if (vp_content.getCurrentItem() == 0)
            return true;
        else
            return false;
    }
    private MyPageChangeListener myPageChangeListener;

    public void setMyPageChangeListener(MyPageChangeListener l) {

        myPageChangeListener = l;

    }

    public interface MyPageChangeListener {
        public void onPageSelected(int position);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
